package cn.wolfcode.shop.web.frontend;

import cn.wolfcode.shop.redis.RedisTemplate;
import cn.wolfcode.shop.redis.SeckillServerKeyPrefix;
import cn.wolfcode.shop.util.VerifyCodeImgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

@RestController
public class VerifyCodeController {

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("getVerifyCodeImage")
    public void getVerifyCodeImage(String uuid, HttpServletResponse response) throws Exception{
        String verifyCode = VerifyCodeImgUtil.generateVerifyCode();
        Integer result = VerifyCodeImgUtil.calc(verifyCode);
        redisTemplate.set(SeckillServerKeyPrefix.VERIFY_RESULT,uuid,result);
        ImageIO.write(VerifyCodeImgUtil.createVerifyCodeImg(verifyCode),"JPEG",response.getOutputStream());
    }
}
